/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						         transition_coordinates.cpp

	$Header: /resource_editor/transition_coordinates.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "transition_coordinates.h"

#include <memory.h>
#include "elements_of.h"

const int k_transition_coordinates[][3] =
{
	{  96,  0,   33 }, { 160,  0,  201 }, { 224,  0,   30 }, { 288,  0, 3333 }, { 416,  0, 1233 },
	{ 480,  0,  300 }, { 544,  0,  300 }, 
	{  64, 16, 3000 }, { 128, 16,   33 }, { 192, 16, 1010 }, { 448, 16,  212 }, { 512, 16,  103 },
	{ 576, 16, 1203 }, { 640, 16,  300 }, 
	{  96, 32, 1030 }, { 160, 32, 3300 }, { 224, 32, 3102 }, { 352, 32, 3023 }, { 480, 32, 2310 },
	{ 544, 32,    3 }, { 544, 32,    3 }, { 608, 32,  312 }, { 672, 32, 1003 },
	{  64, 48, 3000 }, { 128, 48,    2 }, { 192, 48, 2000 }, { 320, 48,   21 }, { 384, 48,  330 },
	{ 640, 48, 3003 }, { 704, 48, 3102 },
	{  96, 64,   20 }, { 160, 64,  200 }, { 224, 64,  330 }, { 288, 64,  121 }, { 416, 64, 3103 },
	{ 544, 64, 3123 }, { 672, 64, 2020 },
	{  64, 80,   31 }, { 128, 80,   33 }, { 192, 80,  210 }, { 256, 80, 2101 }, { 320, 80, 3003 },
	{ 384, 80, 2000 }, { 512, 80, 2121 }, { 640, 80,   21 }, { 704, 80,   30 },
	{  32, 96, 3000 }, {  96, 96, 1000 }, { 224, 96, 2010 }, { 352, 96,   30 }, { 416, 96,  330 },
	{ 480, 96, 2021 }, { 608, 96,   31 }, { 672, 96, 3000 },
	{  64,112,  230 }, { 128,112, 1002 }, { 192,112, 3300 }, { 256,112,  330 }, { 320,112, 3300 },
	{ 448,112,    1 }, { 512,112,   30 }, { 576,112, 3000 }, { 704,112,   30 },
	{  96,128,  310 }, { 160,128,  302 }, { 288,128,  103 }, { 416,128,  300 }, { 480,128, 1200 },
	{ 544,128,  300 }, { 608,128,  120 }, { 672,128, 3300 },
	{ 128,144,    3 }, { 192,144, 3003 }, { 256,144, 2000 }, { 320,144, 3003 }, { 384,144,   33 },
	{ 448,144,    3 }, { 512,144,   12 }, { 576,144, 2301 }, { 640,144,    3 }, 
	{  96,160,  300 }, { 224,160,  230 }, { 288,160,   30 }, { 352,160, 3330 }, { 480,160, 3000 },
	{ 544,160,   33 }, { 672,160,  300 },
	{  64,176,  233 }, { 128,176, 3003 }, { 256,176, 1210 }, { 448,176,  300 }, { 512,176, 3020 },
	{ 640,176,   23 }, { 704,176, 3003 },
	{  96,192,  210 }, { 160,192,  330 }, { 288,192, 1012 }, { 416,192,  233 }, { 480,192,    1 },
	{ 544,192,  330 }, { 608,192,   21 }, { 672,192,  200 }, { 736,192,   30 },
	{ 128,208,   10 }, { 192,208, 3303 }, { 256,208, 3000 }, { 320,208, 1002 }, { 448,208,  310 },
	{ 576,208,  201 }, { 640,208,   33 }, { 704,208, 3310 },
	{  96,224, 3000 }, { 160,224,   33 }, { 288,224,  120 }, { 352,224, 1202 }, { 480,224, 3003 },
	{ 544,224,   33 }, { 608,224, 1210 },
	{ 128,240, 1230 }, { 256,240, 2031 }, { 320,240,    3 }, { 384,240, 1212 }, { 448,240,  300 },
	{ 512,240, 3120 }, { 640,240, 3012 }, { 768,240, 3033 },
	{ 160,256, 1012 }, { 224,256, 3300 }, { 288,256,  330 }, { 416,256,  212 }, { 480,256, 2001 },
	{ 608,256, 3300 }, { 672,256,   30 }, { 736,256, 3000 }, { 800,256,   30 },
	{  64,272, 1033 }, { 128,272, 3300 }, { 192,272,  102 }, { 320,272, 1003 }, { 448,272,  210 },
	{ 512,272,   30 }, { 576,272,  333 }, { 640,272, 1000 }, { 768,272, 3030 },
	{  32,288, 3000 }, {  96,288,    2 }, { 160,288, 2100 }, { 224,288, 3003 }, { 288,288,  100 },
	{ 352,288, 3302 }, { 480,288, 1210 }, { 608,288, 3003 }, { 672,288, 1002 }, { 736,288, 3300 },
	{ 800,288,   30 },
	{  64,304,  330 }, { 128,304, 2300 }, { 256,304, 2030 }, { 320,304,    3 }, { 512,304, 1012 },
	{ 640,304,  330 }, { 704,304,  202 }, { 768,304, 3100 },
	{  96,320,    3 }, { 224,320, 3300 }, { 288,320,   30 }, { 416,320, 3033 }, { 480,320, 3000 },
	{ 544,320, 3002 }, { 672,320,    3 }, { 736,320, 2310 },
	{ 128,336,  300 }, { 192,336,  123 }, { 256,336, 1200 }, { 320,336,  300 }, { 384,336, 3300 },
	{ 448,336,  330 }, { 512,336,   20 }, { 576,336,  330 }, { 640,336,  300 },
	{  96,352,   33 }, { 160,352, 2001 }, { 224,352,    3 }, { 288,352,   12 }, { 352,352,    3 },
	{ 480,352,  301 }, { 608,352,  203 }, { 672,352, 1003 },
	{  64,368, 3000 }, { 192,368,   30 }, { 256,368, 3000 }, { 320,368,  100 }, { 448,368,   33 },
	{ 512,368, 3003 }, { 576,368,   33 }, { 640,368,  210 }, { 704,368, 3002 },
	{  32,384,  300 }, {  96,384,   20 }, { 160,384, 3100 }, { 288,384, 2120 }, { 352,384, 3003 }, 
	{ 416,384, 3000 }, { 544,384, 1020 }, { 672,384,   10 }, { 736,384,   30 }, 
	{   0,400,  233 }, {  64,400,  101 }, { 128,400, 2300 }, { 256,400, 2121 }, { 384,400,   20 },
	{ 448,400,  330 }, { 512,400,  121 }, { 576,400, 1002 }, { 640,400, 3300 }, { 704,400, 3100 },
	{ 768,400, 2100 }, { 832,400,  210 },
	{  32,416, 2010 }, {  96,416,    3 }, { 224,416, 2331 }, { 352,416,  331 }, { 480,416, 2101 },
	{ 544,416, 3003 }, { 608,416,    2 }, { 672,416, 2000 },  
	{   0,432, 3000 }, {  64,432,  330 }, { 128,432,  300 }, { 384,432, 3003 }, { 448,432, 2100 },
	{ 576,432,   20 }, { 640,432,  200 }, { 704,432,   30 }, { 768,432, 1002 }, { 832,432,   21 },
	{  32,448,  330 }, {  96,448,  303 }, { 160,448, 1203 }, { 224,448,  300 }, { 288,448, 1023 },
	{ 416,448, 2330 }, { 544,448,  231 }, { 608,448,   33 }, { 672,448, 3010 },
	{  64,464,    3 }, { 128,464,    3 }, { 192,464,  312 }, { 256,464,  301 }, { 320,464, 1202 },
	{ 576,464, 3310 }, { 640,464, 3000 }, { 704,464,   30 },
	{ 224,480,    3 }, { 288,480,    3 }, { 352,480, 3312 }, { 672,480, 3330 },
	{ 800,384,  400 }, { 736,416, 4000 }, { 864,416,   40 }, { 800,448,    4 },
	{  -1, -1, -1 }
};

// -------------------------------------------------------------------------------
// translate decimal encoded transition code to binary encoded transition code
// -------------------------------------------------------------------------------
int translate_transition_code( int decimal_code )
{
	t_transition_type digit;
	int i;
	int divisor = 1;
	int result = 0;

	for (i = 0; i < 4; i++)
	{
		result <<= 2;
		digit = static_cast<t_transition_type>((decimal_code / divisor) % 10);
		if (digit == k_transition_corner)
			return 259 - i; // codes with a "4" digit are treated specially.
		result |= digit;
		divisor *= 10;
	}
	return result;
}


// --------------------------------------------------------
// return whether a given square is considered "ocean"
// --------------------------------------------------------
static int const k_ocean_codes[] =
{
	  33,   30,  300, 3000, 3300, 3102, 3023, 2310,    3, 1003,    2,
	2000,   21,  330, 3003,   20,  200, 3103,   31,  210, 1000, 2010,
	 230, 1002,    1,  310, 1200,  120,   12,  233,   23,   10, 3310,
	2001, 1033, 2100,  100, 2300, 3100, 3002, 1020, 1023, 2330,  231,
	   4,   40,  400, 4000
};

static int const k_land_codes[] =
{
	 201, 3333, 1233, 1010,  212,  103, 1203, 1030,  312,  121, 3123,
	2020, 2101, 2121, 2021,  302, 2301, 3330, 1210, 3020, 1012, 3303,
	1202, 1230, 2031, 1212, 3120, 3012, 3033,  102,  333, 3030, 3302,
	2030,  202,  123,  301,  203, 2120,  101, 2331,  331,  303, 3010,
	3312,    4,   40,  400, 4000
};

static void set_codes( bool* array, int const* codes, int size )
{
	int id;

	memset( array, 0, 260 );
	while (size--)
	{
		id = translate_transition_code( codes[size] );
		array[id] = true;
	}
}


bool is_ocean_transition_code( int binary_code )
{
	static bool initialized = false;
	static bool	codes[260];

	if (!initialized)
	{
		initialized = true;
		set_codes( codes, k_ocean_codes, ELEMENTS_OF( k_ocean_codes ));
	}
	return codes[binary_code];
}

// --------------------------------------------------------
// return whether a given square is considered "land"
// --------------------------------------------------------
bool is_land_transition_code( int binary_code )
{
	static bool initialized = false;
	static bool	codes[260];

	if (!initialized)
	{
		initialized = true;
		set_codes( codes, k_land_codes, ELEMENTS_OF( k_land_codes ));
	}
	return codes[binary_code];
}


// --------------------------------------------------------
// return whether a given square is considered "river"
// --------------------------------------------------------
static int const k_river_codes[] =
{
	 201, 1233, 1010,  212,  103, 1203, 1030,  312,  121,
	3123, 2020, 2101, 2121, 2010, 2021,  302, 1200,  120,
	  12, 2301, 3330, 1210, 3020, 1012, 3303, 3310, 1202,
	1230, 2031, 1212, 3120, 3012, 2001,  102, 3030, 3302,
	2030,  202,  123,  301,  203, 2120, 1020,  101, 2331,
	 331,  303, 3010, 3312
};
	
bool is_river_transition_code( int binary_code )
{
	static bool initialized = false;
	static bool	codes[260];

	if (!initialized)
	{
		initialized = true;
		set_codes( codes, k_river_codes, ELEMENTS_OF( k_river_codes ));
	}
	return codes[binary_code];
}

